package top.yxqz.servlet;


import top.yxqz.user.Student;
import top.yxqz.util.JDBC;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;

/**
 * @author Mr.余
 * @date 2022/11/16
 * @package top.yxqz.servlet
 */
@WebServlet("/studentregist")
public class StudentResistServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = getServletContext();
        String req_coding = context.getInitParameter("req_coding");
        String resp_coding = context.getInitParameter("resp_coding");
        request.setCharacterEncoding(req_coding);
        response.setContentType(resp_coding);

        PrintWriter writer = response.getWriter();
        writer.println("hello student");
//        从前端获取数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        writer.println("<hr>");
        writer.println("前台传入的username:" + username);
        writer.println("前台传入的password:" + password);

        Student student = new Student();
        student.setUserName(username);
        student.setPassword(password);
//        连接mysql
        try {
//            JDBC来自于本项目中的jdbc模块
            Connection connection = JDBC.getConnection();
            if (connection!=null){
//                调用注册的方法
                int res   = regist(connection,student);
//                注册
                if (res>0){
                    writer.println("<hr>");
                    writer.println("【注册成功~】");
                }else {
                    writer.println("<hr>");
                    writer.println("【注册失败！！！！】");
                }

            }
        } catch (SQLException e) {
            writer.println("<hr>");
            writer.println("你的JDBC连接失败，请查看后端（StudentServlet）的第41行代码");
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 注册功能
     * @param connection 数据库连接对象
     * @param student 注册的对象数据
     * @return  注册的结果
     */
    private int regist(Connection connection, Student student) throws SQLException {
        String zhuce_sql = "insert into stu(username,password)values (?,?)";
        PreparedStatement ps2 = connection.prepareStatement(zhuce_sql);
        ps2.setString(1,student.getUserName());
        ps2.setString(2,student.getPassword());
        int i = ps2.executeUpdate();
        return  i;
    }
}
